package cn.edu.lingnan.servlet;

import cn.edu.lingnan.pojo.Admin;
import cn.edu.lingnan.pojo.User;
import cn.edu.lingnan.service.userservice;
import cn.edu.lingnan.service.userserviceimp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


public class RegisterServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        userservice us=new userserviceimp();
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String name=req.getParameter("name");
       User user=new User();
       user.setUsername(username);
       user.setPassword(password);
       user.setName(name);
        System.out.println(user);
        HttpSession session = req.getSession();
        if(user!=null&&user.getUsername()!=""){
            Integer register = us.register(user);
            if(register==1){
//                session.setAttribute("register_message","注册成功！");
                System.out.println("注册成功！");
                resp.getWriter().print("<script>alert('注册成功');window.location.href='login.jsp';</script>");
            }else{
                System.out.println("注册失败！");
                session.setAttribute("register_message","注册失败！");
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
            }
        }
        else{
            session.setAttribute("register_message","用户名或密码不能为空！");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }


    }
}